package com.chinasoft.mall.activity.adapter;

import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.subActivity.ProgramListActivity;
import com.chinasoft.mall.activity.subActivity.goods.GoodsDetailActivity;
import com.chinasoft.mall.framework.LivingGoods;
import com.chinasoft.mall.model.response.EpgBean;
import com.chinasoft.mall.util.DateUtil;
import com.chinasoft.mall.util.LocalDataCommend;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.util.SystemSetting;
import com.chinasoft.mall.widget.MySlipSwitch;
import com.chinasoft.mall.widget.MySlipSwitch.OnSwitchListener;
import com.chinasoft.mall.widget.NetImageView;

public class JmdListAdapter extends BaseAdapter {
	private List<EpgBean> dds;
	private LayoutInflater inflater;
	private ProgramListActivity JmdActivity;
	private ViewHolder _vh;

	public JmdListAdapter(ProgramListActivity JmdActivity, List<EpgBean> crs) {
		inflater = LayoutInflater.from(JmdActivity);
		this.JmdActivity=JmdActivity;
		this.dds = crs;
	}
	
	public void setData(List<EpgBean> crs){
		this.dds=crs;
		this.notifyDataSetChanged();
	}
	
	public List<EpgBean> getData(){
		return dds;
	}
	
	@Override
	public int getCount() {
		if (dds == null) {
			return 0;
		} else {
			return dds.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (dds == null) {
			return null;
		} else {
			return dds.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.jmd_lv, null);
			_vh = new ViewHolder();
			_vh.right_rl=(RelativeLayout) convertView.findViewById(R.id.right_rl);
			_vh.play_rl=(LinearLayout) convertView.findViewById(R.id.play_rl);
			_vh.modelimg=(NetImageView) convertView.findViewById(R.id.modelimg);
			_vh.spid=(TextView) convertView.findViewById(R.id.spid);
			_vh.sptitle=(TextView) convertView.findViewById(R.id.sptitle);
			_vh.newprice=(TextView) convertView.findViewById(R.id.newprice);
			_vh.oldprice=(TextView) convertView.findViewById(R.id.oldprice);
			_vh.playing_tv=(RelativeLayout) convertView.findViewById(R.id.playing_tv);
			_vh.date=(TextView) convertView.findViewById(R.id.date);
			_vh.myslipswitch=(MySlipSwitch) convertView.findViewById(R.id.myslipswitch);
			_vh.myslipswitch.setImageResource(R.drawable.bkg_switchon, R.drawable.bkg_switchoff, R.drawable.btn);
			convertView.setTag(_vh);
		} else {
			_vh = (ViewHolder) convertView.getTag();
		}
		EpgBean _dd=dds.get(position);
		_vh.myslipswitch.updateSwitchState(_dd.isNotify());
		_vh.modelimg.setDownloadurl(StringUtil.goodUrl(_dd.getGOODS_ID(),_dd.getTHUM()));
		_vh.spid.setText("19"+_dd.getGOODS_ID());
		_vh.sptitle.setText(_dd.getGOODS_NAME());
		_vh.newprice.setText("￥"+_dd.getPRC());
		_vh.oldprice.setText("￥"+_dd.getMPRC());
		_vh.date.setText(DateUtil.format2Date(_dd.getFORM_FR_DATE(),_dd.getFORM_END_DATE()));
		if(_dd.getLivingState()==0){
			_vh.playing_tv.setVisibility(View.VISIBLE);
			_vh.play_rl.setVisibility(View.GONE);
		}else{
			_vh.playing_tv.setVisibility(View.GONE);
			_vh.play_rl.setVisibility(View.VISIBLE);
		}
		_vh.right_rl.setOnClickListener(new onClickImpl(JmdActivity, dds, position));
		_vh.myslipswitch.setOnSwitchListener(new onTouchImpl(_dd,position));
		return convertView;
	}
	
	class onTouchImpl implements OnSwitchListener  {
		private EpgBean _dd;
		private int position;
		public onTouchImpl(EpgBean _dd,int position){
			this._dd=_dd;
			this.position=position;
		}
		@Override
		public void onSwitched(boolean isSwitchOn) {
			if(!_dd.isNotify()==isSwitchOn){
				if(isSwitchOn){
					LocalDataCommend.addGoodsSelect(JmdActivity, _dd.getGOODS_ID());
				}
				else{
					LocalDataCommend.removeGoodsSelect(JmdActivity, _dd.getGOODS_ID());
				}
				SystemSetting.goodsids=LocalDataCommend.getGoodsSelect(JmdActivity);
				_dd.setNotify(isSwitchOn);
				if(JmdActivity.jdmState==1){
					LivingGoods.getInstance().getEpgBeans().set(position, _dd);
				}
				LivingGoods.getInstance().invalidate();
			}
		}
	}
	
	/**
	 * 跳转到通道详情
	 * 
	 * @author Administrator
	 * 
	 */
	class onClickImpl implements View.OnClickListener {
		private List<EpgBean> crs;
		private int position;
		private ProgramListActivity JmdActivity;

		public onClickImpl(ProgramListActivity JmdActivity, List<EpgBean> crs, int position) {
			this.crs = crs;
			this.position = position;
			this.JmdActivity = JmdActivity;
		}

		@Override
		public void onClick(View v) {
			EpgBean scd = crs.get(position);
			if (scd != null) {
				((MainActivity)JmdActivity.getParent()).setResultSz(JmdActivity.jdmState+"");
				((MainActivity)JmdActivity.getParent()).setCurrentActivity(GoodsDetailActivity.class, scd);
			}
		}
	}

	class ViewHolder {
		public NetImageView modelimg;
		public RelativeLayout right_rl;
		public LinearLayout play_rl;
		public TextView date;
		public TextView spid;
		public TextView sptitle;
		public TextView newprice;
		public TextView oldprice;
		public RelativeLayout playing_tv;
		public MySlipSwitch myslipswitch;
	}
}
